import type {
  KnowledgeParams,
  KnowledgePage,
  PageParams,
  DoctorPage,
  FirstDept,
  ConsultOrderPreData,
  Consult,
  ConsultOrderItem,
  ConsultOrderListParams,
  ConsultOrderPage,
} from '@/types/consult';
import type { Data } from '@/types/user';
import { instance as request } from '@/utils/request';

// 获取文章列表
export const getKnowledgePage = async (params: KnowledgeParams) => {
  const res = await request.get<KnowledgePage, Data<KnowledgePage>>(
    '/patient/home/knowledge',
    { params }
  );
  return res.data;
};

// 获取关注医生列表
export const getDoctorPage = async (params: PageParams) => {
  const res = await request.get<DoctorPage, Data<DoctorPage>>(
    '/home/page/doc',
    { params }
  );
  return res.data;
};

// 关注医生
export const followDoctor = async (id: string) => {
  const res = await request.post('/like', { id, type: 'doc' });
  return res.data;
};

// 关注文章
export const followKnowledge = async (id: string) => {
  const res = await request.post('/like', { id, type: 'knowledge' });
  return res.data;
};

// 获取科室数据
export const getAllDept = async () => {
  const res = await request.get<FirstDept[], Data<FirstDept[]>>('/dep/all');
  return res.data;
};

// 图片上传网络请求封装
export const uploadImage = async (file: File) => {
  // 把二进制file文件放到 FormData 对象中
  const fd = new FormData();
  fd.append('file', file);
  const res = await request.post<
    { id: string; url: string },
    Data<{ id: string; url: string }>
  >('/upload', fd);
  return res.data;
};

// 获取订单准备数据
export const getConsultOrderPre = async (
  type: 1 | 2 | 3,
  illnessType: 0 | 1
) => {
  const res = await request.get<ConsultOrderPreData, Data<ConsultOrderPreData>>(
    '/patient/consult/order/pre',
    {
      params: { type, illnessType },
    }
  );
  return res.data;
};

// 生成订单
export const createConsultOrder = async (data: Consult) => {
  const res = await request.post<{ id: string }, Data<{ id: string }>>(
    '/patient/consult/order',
    data
  );
  return res.data;
};

// 请求支付地址
export const getConsultOrderPayUrl = async (data: {
  paymentMethod: 0 | 1;
  orderId: string;
  payCallback: string;
}) => {
  const res = await request.post<{ payUrl: string }, Data<{ payUrl: string }>>(
    '/patient/consult/pay',
    data
  );
  return res.data;
};

// 获取订单详情
export const getConsultOrderDetail = async (orderId: string) => {
  const res = await request.get<ConsultOrderItem, Data<ConsultOrderItem>>(
    '/patient/consult/order/detail',
    {
      params: { orderId },
    }
  );
  return res.data;
};

// 根据处方id获取处方图片url地址
export const getPrescriptionPic = async (id: string) => {
  const res = await request.get<{ url: string }, Data<{ url: string }>>(
    `/patient/consult/prescription/${id}`
  );
  return res.data;
};

// 获取问诊记录（订单）列表
export const getConsultOrderList = async (params: ConsultOrderListParams) => {
  const res = await request.get<ConsultOrderPage, Data<ConsultOrderPage>>(
    '/patient/consult/order/list',
    {
      params,
    }
  );
  return res.data;
};

// 删除订单
export const deleteOrder = async (id: string) => {
  const res = await request.delete(`/patient/order/${id}`);
  return res.data;
};

// 取消订单
export const cancelOrder = async (id: string) => {
  const res = await request.put(`/patient/order/cancel/${id}`);
  return res.data;
};
